home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
cmprss.exe
/
DECPRESS.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-01-26
|
2KB
|
169 lines
#include "decpress.cls"
cpifstream::cpifstream(void) : idx(0), o_len(0), all_done(0), count(0)
{
}
cpifstream::cpifstream(char *fn) : idx(0), o_len(0), all_done(0), count(0)
{
init();
ifstream::open(fn, ios::binary | ios::in);
flags(0); // clear all flags (especially the skip white space flag)
}
cpifstream::~cpifstream()
{
clear();
}
cpifstream& cpifstream::operator>>(char* s)
{
char c;
do
{
if (idx==o_len)
decode();
*s=c=outbuf[idx++];
s++;
count++;
} while (c);
return *this;
}
cpifstream& cpifstream::ignore(int n, int delim)
{
if (!n)
return *this;
char c;
do
{
c=get();
} while ( (c!=delim) && (--n!=0) && (!all_done) );
return *this;
}
int cpifstream::get(void)
{
if (all_done)
return EOF;
unsigned char c;
getbuf(&c,sizeof(unsigned char));
count++;
return c;
}
int cpifstream::peek(void)
{
if (all_done)
return EOF;
if (idx==o_len)
decode();
return outbuf[idx];
}
void cpifstream::open(char *fn)
{
init();
ifstream::open(fn, ios::binary | ios::in);
flags(0); // clear all flags (especially the skip white space flag)
all_done=0;
}
filebuf* cpifstream::close(void)
{
clear();
return rdbuf()->close();
}
void cpifstream::init(void)
{
inbuf=new unsigned char[CBUF_SIZE];
outbuf=new unsigned char[CBUF_SIZE];
}
void cpifstream::clear(void)
{
delete inbuf;
delete outbuf;
}
void cpifstream::decode(void)
{
idx=0;
istream::read((char*)&o_len,2);
if (o_len==0)
{
all_done=1;
return;
}
if (o_len<0)
{
o_len=0-o_len;
istream::read(outbuf,o_len);
}
else
{
istream::read(inbuf,o_len);
o_len=decompress(inbuf,o_len,outbuf);
}
}
void cpifstream::getbuf(void* v, int n)
{
while (n--)
{
if (idx==o_len)
decode();
*(char*)v=outbuf[idx++];
((char*)v)++;
count++;
}
}
void cpifstream::getbuf2(char* s, int l, char delim, char ed)
{
if (all_done)
return;
if (l<2) // read l-1 bytes, so l must be 2 or more
{
*s='\0';
return;
}
char c;
do
{
if (!ed) // if we aren't extracting the delimeter
{
if (peek()==delim) // don't extract delimiter
{
s++; // necessary for s-- outside of loop
break;
}
}
*s++=c=get();
} while ( (c!=delim) && (--l!=0) && (!all_done) );
s--;
*s='\0';
}